#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys

execfile(os.path.join(sys.path[0], 'framework.py'))

from oriolus.pith.packets.version import Version, MaxVersion
from oriolus.pith.packets.versionrange import VersionRange, VersionRangeException
from unittest import TestCase, TestLoader, TextTestRunner

class TestVersionRange(TestCase):
    def test_creation01(self):
        range = VersionRange("(1.2.3.4, 2.3.4.5)")
        self.assert_(cmp(Version("1.2.3.4"), range.min_version)==0)
        self.assert_(cmp(Version("2.3.4.5"), range.max_version)==0)

    def test_creation02(self):
        range = VersionRange("[2.3.4.5, 5.2.3.4]")
        self.assert_(cmp(Version("2.3.4.5"), range.min_version)==0)
        self.assert_(cmp(Version("5.2.3.4"), range.max_version)==0)
        
    def test_creation03(self):		
        self.assertRaises(VersionRangeException, VersionRange, "[5.4.3.2, 1.2.3.4]")
        
    def test_creation04(self):
        self.assertRaises(VersionRangeException, VersionRange, "[5.2., 2.3.4.5]")

    def test_creation05(self):
        range = VersionRange("2.3.4.5")
        self.assert_(cmp(Version("2.3.4.5"), range.min_version)==0)
        self.assert_(cmp(Version("2.3.4.5"), range.max_version)==0)
        
    def test_creation06(self):
        range = VersionRange("(2.3.4.5,)")
        self.assert_(cmp(Version("2.3.4.5"), range.min_version)==0)
        self.assert_(isinstance(range.max_version, MaxVersion))

    def test_compare01(self):
        range = VersionRange("(1.3.4.5, 2.1.2.3)")
        self.assert_(cmp(Version("1.3.4.5"), range.min_version)==0)
        self.assert_(cmp(Version("2.1.2.3"), range.max_version)==0)
        self.assert_(range.contains(Version("2.0")))

    def test_compare02(self):
        range = VersionRange("(1.3.4.5, 2.1.2.3)")
        self.assert_(cmp(Version("1.3.4.5"), range.min_version)==0)
        self.assert_(cmp(Version("2.1.2.3"), range.max_version)==0)
        self.assert_(not range.contains(Version("3")))

    def test_compare03(self):
        range = VersionRange("(1.3.4.5, 2.1.2.3)")
        self.assert_(cmp(Version("1.3.4.5"), range.min_version)==0)
        self.assert_(cmp(Version("2.1.2.3"), range.max_version)==0)
        self.assert_(not range.contains(Version("1.3.4.5")))

    def test_compare04(self):
        range = VersionRange("[1.3.4.5, 2.1.2.3)")
        self.assert_(cmp(Version("1.3.4.5"), range.min_version)==0)
        self.assert_(cmp(Version("2.1.2.3"), range.max_version)==0)		
        self.assert_(range.contains(Version("1.3.4.5")))

    def test_compare05(self):
        range = VersionRange("[1.3.4.5, 2.1.2.3)")
        self.assert_(cmp(Version("1.3.4.5"), range.min_version)==0)
        self.assert_(cmp(Version("2.1.2.3"), range.max_version)==0)
        self.assert_(not range.contains(Version("2.1.2.3")))

    def test_compare06(self):
        range = VersionRange("[1.3.4.5, 2.1.2.3]")
        self.assert_(cmp(Version("1.3.4.5"), range.min_version)==0)
        self.assert_(cmp(Version("2.1.2.3"), range.max_version)==0)
        self.assert_(range.contains(Version("2.1.2.3")))

    def test_compare07(self):
        range = VersionRange("(2.3.4.5,)")
        self.assert_(cmp(Version("2.3.4.5"), range.min_version)==0)		
        self.assert_(range.contains(Version("2.4")))

    def test_compare08(self):
        range = VersionRange("(,2.3.4.5)")		
        self.assert_(cmp(Version("2.3.4.5"), range.max_version)==0)
        self.assert_(range.contains(Version("1")))
        
    def test_compare09(self):
        range = VersionRange("(,2.3.4.5)")		
        self.assert_(cmp(Version("2.3.4.5"), range.max_version)==0)
        self.assert_(not range.contains(Version("3")))
        
    def test_compare10(self):
        range = VersionRange("(2.3.4.5,)")
        self.assert_(cmp(Version("2.3.4.5"), range.min_version)==0)		
        self.assert_(not range.contains(Version("1")))
    
    def test_compare11(self):
        range = VersionRange("[2.0.0.0, )")
        self.assertTrue(not range.contains(Version("1.2.3.4")))
        
    def test_compare12(self):
        range = VersionRange("( , 1.0.0.0]")
        self.assertTrue(not range.contains(Version("1.2.3.4")))
        
    def test_compare13(self):
        range = VersionRange("1.2")
        self.assertTrue(range.contains(Version("1.2.3.4")))

    def test_compare14(self):
        range = VersionRange("1.2.")
        self.assertTrue(range.contains(Version("1.2.3.4")))
        
    def test_empty_creation_and_compare(self):
        empty_version_range_presentations = ["[]", "()", "(]", "[)",
                                             "[,]", "(,)", "(,]", "[,)",
                                                "[ ]", "( )", "( ]", "[ )",
                                             "[, ]", "(, )", "(, ]", "[, )",
                                             "[ ,]", "( ,)", "( ,]", "[ ,)",
                                             "[ ,  ]", "(   ,)", "( , ]", "[  ,   )"]

        any_version_samples = [Version("1.2.3.4"),
                               Version("999.999.999.999"),
                               Version("0.0.0.0"),
                               Version("9000.0.0.1"),
                               Version("0"),
                               Version("10.100"),
                               Version("999.99.9")]

        for empty_version_range_string in empty_version_range_presentations:
            empty_version_range = VersionRange(empty_version_range_string)
            self.assert_(cmp(Version.Any(), empty_version_range.min_version)==0)
            self.assert_(cmp(Version.Any(), empty_version_range.max_version)==0)
            for version in any_version_samples:
                self.assert_(empty_version_range.contains(version))

if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestVersionRange)
    TextTestRunner(verbosity=2).run(suite)